package org.gd.hellojaxb.utils;

import java.io.*;
import java.util.Arrays;
import java.util.Random;

public class StringUtils {

    private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toLowerCase();
    //private static final String ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    //private static final char[] ALPHABET_LIST = ALPHABET.toUpperCase().toCharArray();

    public static InputStream StringToInputStream(String str) {
        return new ByteArrayInputStream(str.getBytes());
    }

    @SuppressWarnings("unused")
    public static String OutputStreamToString(OutputStream outputStream) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byteArrayOutputStream.writeTo(outputStream);
        return new String(byteArrayOutputStream.toByteArray());
    }

    public static String generateString(Random rng, String characters, int length) {
        char[] text = new char[length];
        for (int i = 0; i < length; i++) {
            text[i] = characters.charAt(rng.nextInt(characters.length()));
        }
        return new String(text);
    }

    public static String format(String str) {
        char[] chars = str.toCharArray();
        for(int i=0, j=1; i<chars.length-1 && j<chars.length; i++, j++) {
            if(chars[i] == '5') {
                System.out.println(Arrays.copyOfRange(chars, 0, i));
                System.out.println(Arrays.copyOfRange(chars, j, chars.length));
            }
        }
        return "";
    }

    public static void main(String...strings) {

        System.out.println(generateString(new Random(), ALPHABET, 2));

        //format("123456789");
    }

}
